package leetcode_core.leetcode_4;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2022年11月07日 15:42
 */
public class AmbiguousCoordinates {
    public List<String> ambiguousCoordinates(String s) {
        List<String> res = new ArrayList<>();
        //先对s去除左右括号
        s = s.replace("(", "");
        s = s.replace(")","");
        //然后进行分组
        int len = s.length();
        for(int i = 1;i <len ;i++ ) {
            String x = s.substring(0, i);//获得x轴
            //在x轴的基础之上组合y轴
            //要实现x轴,y轴这样的情况
            //获取x的每一个分组
            List<String> deals = deals(x);
            for (String deal : deals) {
                List<String> allY = deals(s.substring(i));
                for (String s1 : allY) {
                    res.add(new StringBuilder("(").append(deal).append(",").append(" ").append(s1).append(")").toString());
                }
            }
        }
        return res;
    }

    private List<String> deals(String num){
        //在每一个num的合法位置安放
        List<String> lists = new ArrayList<>();
        int i = 1;
        int len = num.length();
        String left,right;
        while (i <= len){
            //同样分割
            left = num.substring(0,i);
            right = num.substring(i);
            i++;//步进
            if((!left.equals("0") && left.charAt(0) == '0') ||
                    (!right.isEmpty() && right.charAt(right.length()-1) == '0')){
                continue;
            }
            if(right.isEmpty()){
                lists.add(left);
            }else{
                lists.add(new StringBuilder(left).append(".").append(right).toString());
            }
        }
        return lists;
    }

    /*
    public static void main(String[] args) {
        String s = "0123456789";
        System.out.println(s.substring(0, 6));//不包含6
        System.out.println(s.substring(5, 9));//不包含9
        System.out.println(s.substring(5));//从5到最后一个
    }
    */

}


